package com.todo.reward.infrastructure.repository.impl;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.reward.common.enums.RewardTypeEnum;
import com.todo.reward.infrastructure.convert.RewardTemplateConvert;
import com.todo.reward.infrastructure.entity.condition.RewardTemplateCondition;
import com.todo.reward.infrastructure.entity.domain.BaseTemplateDO;
import com.todo.reward.infrastructure.entity.domain.CouponExtensionDO;
import com.todo.reward.infrastructure.entity.domain.GrowthExtensionDO;
import com.todo.reward.infrastructure.entity.domain.PointsExtensionDO;
import com.todo.reward.infrastructure.entity.po.RewardCouponTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardGrowthTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardPointsTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardTemplatePO;
import com.todo.reward.infrastructure.mapper.RewardCouponTemplateMapper;
import com.todo.reward.infrastructure.mapper.RewardGrowthTemplateMapper;
import com.todo.reward.infrastructure.mapper.RewardPointsTemplateMapper;
import com.todo.reward.infrastructure.mapper.RewardTemplateMapper;
import com.todo.reward.infrastructure.repository.RewardTemplateRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/2/12 22:11
 * @description
 */
@Repository
public class RewardTemplateRepositoryImpl implements RewardTemplateRepository {

    @Resource
    private RewardPointsTemplateMapper rewardPointsTemplateMapper;
    @Resource
    private RewardGrowthTemplateMapper rewardGrowthTemplateMapper;
    @Resource
    private RewardCouponTemplateMapper rewardCouponTemplateMapper;
    @Resource
    private RewardTemplateMapper rewardTemplateMapper;


    @Override
    @Transactional
    public <T> Boolean insertRewardTemplate(BaseTemplateDO<T> baseTemplate) {
        if (baseTemplate == null || baseTemplate.getExtension() == null) {
            return false;
        }
        RewardTemplatePO rewardTemplatePO = RewardTemplateConvert.buildRewardTemplatePO(baseTemplate);
        int inserted = rewardTemplateMapper.insertRewardTemplate(rewardTemplatePO);
        if (inserted < 1) {
            return false;
        }
        switch (RewardTypeEnum.getByType(baseTemplate.getRewardType())) {
            case POINTS:
                PointsExtensionDO pointsExtensionDO = (PointsExtensionDO) baseTemplate.getExtension();
                RewardPointsTemplatePO rewardPointsTemplatePO = RewardTemplateConvert.buildRewardPointsTemplatePO(pointsExtensionDO, baseTemplate.getId());
                return rewardPointsTemplateMapper.insertRewardPointsTemplate(rewardPointsTemplatePO) > 0;
            case GROWTH:
                GrowthExtensionDO growthExtensionDO = (GrowthExtensionDO) baseTemplate.getExtension();
                RewardGrowthTemplatePO rewardGrowthTemplatePO = RewardTemplateConvert.buildRewardGrowthTemplatePO(growthExtensionDO, baseTemplate.getId());
                return rewardGrowthTemplateMapper.insertRewardGrowthTemplate(rewardGrowthTemplatePO) > 0;
            case COUPON:
                CouponExtensionDO couponExtensionDO = (CouponExtensionDO) baseTemplate.getExtension();
                RewardCouponTemplatePO rewardCouponTemplatePO = RewardTemplateConvert.buildRewardCouponTemplatePO(couponExtensionDO, baseTemplate.getId());
                return rewardCouponTemplateMapper.insertRewardCouponTemplate(rewardCouponTemplatePO) > 0;
            default:
                return false;
        }
    }

    @Override
    @Transactional
    public <T> Boolean updateRewardTemplate(BaseTemplateDO<T> baseTemplate) {
        if (baseTemplate == null || baseTemplate.getExtension() == null) {
            return false;
        }
        RewardTemplatePO rewardTemplatePO = RewardTemplateConvert.buildRewardTemplatePO(baseTemplate);
        int inserted = rewardTemplateMapper.updateRewardTemplate(rewardTemplatePO);
        if (inserted < 1) {
            return false;
        }
        switch (RewardTypeEnum.getByType(baseTemplate.getRewardType())) {
            case POINTS:
                PointsExtensionDO pointsExtensionDO = (PointsExtensionDO) baseTemplate.getExtension();
                RewardPointsTemplatePO rewardPointsTemplatePO = RewardTemplateConvert.buildRewardPointsTemplatePO(pointsExtensionDO, baseTemplate.getId());
                return rewardPointsTemplateMapper.updateRewardPointsTemplate(rewardPointsTemplatePO) > 0;
            case GROWTH:
                GrowthExtensionDO growthExtensionDO = (GrowthExtensionDO) baseTemplate.getExtension();
                RewardGrowthTemplatePO rewardGrowthTemplatePO = RewardTemplateConvert.buildRewardGrowthTemplatePO(growthExtensionDO, baseTemplate.getId());
                return rewardGrowthTemplateMapper.updateRewardGrowthTemplate(rewardGrowthTemplatePO) > 0;
            case COUPON:
                CouponExtensionDO couponExtensionDO = (CouponExtensionDO) baseTemplate.getExtension();
                RewardCouponTemplatePO rewardCouponTemplatePO = RewardTemplateConvert.buildRewardCouponTemplatePO(couponExtensionDO, baseTemplate.getId());
                return rewardCouponTemplateMapper.updateRewardCouponTemplate(rewardCouponTemplatePO) > 0;
            default:
                return false;
        }
    }

    @Override
    public Boolean removeRewardTemplateById(Long templateId) {
        if (templateId == null || templateId <= 0) {
            return false;
        }
        int removed = rewardTemplateMapper.removeRewardTemplateById(templateId);
        return removed > 0;
    }

    @Override
    public <T> BaseTemplateDO<T> queryRewardTemplateById(Long templateId) {
        if (templateId == null || templateId <= 0) {
            return null;
        }
        RewardTemplatePO rewardTemplatePO = rewardTemplateMapper.queryRewardTemplateById(templateId);
        if (rewardTemplatePO == null) {
            return null;
        }
        switch (RewardTypeEnum.getByType(rewardTemplatePO.getRewardType())) {
            case POINTS:
                RewardPointsTemplatePO rewardPointsTemplatePO = rewardPointsTemplateMapper.queryRewardPointsTemplateById(templateId);
                if (rewardPointsTemplatePO == null) {
                    break;
                }
                return (BaseTemplateDO<T>) RewardTemplateConvert.buildBaseTemplateDO(rewardPointsTemplatePO, rewardTemplatePO);
            case GROWTH:
                RewardGrowthTemplatePO rewardGrowthTemplatePO = rewardGrowthTemplateMapper.queryRewardGrowthTemplateById(templateId);
                if (rewardGrowthTemplatePO == null) {
                    break;
                }
                return (BaseTemplateDO<T>) RewardTemplateConvert.buildBaseTemplateDO(rewardGrowthTemplatePO, rewardTemplatePO);
            case COUPON:
                RewardCouponTemplatePO rewardCouponTemplatePO = rewardCouponTemplateMapper.queryRewardCouponTemplateById(templateId);
                if (rewardCouponTemplatePO == null) {
                    break;
                }
                return (BaseTemplateDO<T>) RewardTemplateConvert.buildBaseTemplateDO(rewardCouponTemplatePO, rewardTemplatePO);
            default:
                return null;
        }
        return null;
    }

    @Override
    public PageResult<BaseTemplateDO<PointsExtensionDO>> queryPointsRewardTemplateByCondition(RewardTemplateCondition condition) {
        if (condition == null) {
            return null;
        }
        if (!Objects.equals(condition.getRewardType(), RewardTypeEnum.POINTS.getType())) {
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL, "奖励类型为空");
        }
        List<RewardTemplatePO> rewardTemplatePOS = rewardTemplateMapper.queryRewardTemplateByCondition(condition);
        if (CollectionUtils.isEmpty(rewardTemplatePOS)) {
            return PageResult.of(new ArrayList<>(), condition.getPageIndex(), condition.getPageSize(), 0);
        }
        int count = rewardTemplateMapper.queryRewardTemplateCount(condition);
        List<Long> idList = rewardTemplatePOS.stream().map(RewardTemplatePO::getId).collect(Collectors.toList());

        List<RewardPointsTemplatePO> rewardPointsTemplatePOList = rewardPointsTemplateMapper.queryRewardPointsTemplateByIdList(idList);
        return PageResult.of(RewardTemplateConvert.buildPointBaseTemplateDOList(rewardPointsTemplatePOList, rewardTemplatePOS), condition.getPageIndex(), condition.getPageSize(), count);
    }

    @Override
    public PageResult<BaseTemplateDO<GrowthExtensionDO>> queryGrowthRewardTemplateByCondition(RewardTemplateCondition condition) {
        if (condition == null) {
            return null;
        }
        if (!Objects.equals(condition.getRewardType(), RewardTypeEnum.GROWTH.getType())) {
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL, "奖励类型为空");
        }
        List<RewardTemplatePO> rewardTemplatePOS = rewardTemplateMapper.queryRewardTemplateByCondition(condition);
        if (CollectionUtils.isEmpty(rewardTemplatePOS)) {
            return PageResult.of(new ArrayList<>(), condition.getPageIndex(), condition.getPageSize(), 0);
        }
        int count = rewardTemplateMapper.queryRewardTemplateCount(condition);
        List<Long> idList = rewardTemplatePOS.stream().map(RewardTemplatePO::getId).collect(Collectors.toList());

        List<RewardGrowthTemplatePO> rewardGrowthTemplatePOList = rewardGrowthTemplateMapper.queryRewardGrowthTemplateByIdList(idList);
        return PageResult.of(RewardTemplateConvert.buildGrowthBaseTemplateDOList(rewardGrowthTemplatePOList, rewardTemplatePOS), condition.getPageIndex(), condition.getPageSize(), count);
    }

    @Override
    public PageResult<BaseTemplateDO<CouponExtensionDO>> queryCouponRewardTemplateByCondition(RewardTemplateCondition condition) {
        if (condition == null) {
            return null;
        }
        if (!Objects.equals(condition.getRewardType(), RewardTypeEnum.COUPON.getType())) {
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL, "奖励类型为空");
        }
        List<RewardTemplatePO> rewardTemplatePOS = rewardTemplateMapper.queryRewardTemplateByCondition(condition);
        if (CollectionUtils.isEmpty(rewardTemplatePOS)) {
            return PageResult.of(new ArrayList<>(), condition.getPageIndex(), condition.getPageSize(), 0);
        }
        int count = rewardTemplateMapper.queryRewardTemplateCount(condition);
        List<Long> idList = rewardTemplatePOS.stream().map(RewardTemplatePO::getId).collect(Collectors.toList());

        List<RewardCouponTemplatePO> rewardCouponTemplatePOList = rewardCouponTemplateMapper.queryRewardCouponTemplateByIdList(idList);
        return PageResult.of(RewardTemplateConvert.buildCouponBaseTemplateDOList(rewardCouponTemplatePOList, rewardTemplatePOS), condition.getPageIndex(), condition.getPageSize(), count);
    }


}
